﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using EquipmentConfig.Class;

namespace EquipmentConfig
{
    public partial class DebugInfo : Form
    {
        private MainForm mf;
        private int dType;
        private int dIndex;

        public DebugInfo(MainForm mainForm, int debugType, int index)
        {
            InitializeComponent();
            mf = mainForm;
            dType = debugType;
            dIndex = index;
            mf.recBuffer.Clear();
            rtbDebugMessage.Clear();
        }

        /// <summary>
        /// 显示信息
        /// </summary>
        /// <param name="msg">信息</param>
        public void ShowMessage(string msg, int type, int index)
        {
            if (type == dType && index == dIndex)
            {
                rtbDebugMessage.Invoke((MethodInvoker)delegate
                {
                    if (rtbDebugMessage.Lines.Length > 100)
                    {
                        rtbDebugMessage.Clear();
                    }
                    rtbDebugMessage.AppendText(msg);//添加内容
                    rtbDebugMessage.Focus();
                    rtbDebugMessage.ScrollToCaret();
                });
            }
        }

        /// <summary>
        /// 开始调试
        /// </summary>
        private void btnDebugRun_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                btnDebugRun.Enabled = false;
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetDeviceDebugCmd(dType, dIndex, true));
                Thread sub = new Thread(() =>
                {
                    if (!mf.CheckReturnCmd())
                    {
                        Messages.ShowAlertDialog("开始调试操作失败！", false);
                        this.Invoke((MethodInvoker)delegate
                        {
                            btnDebugRun.Enabled = true;
                        });
                    }
                    //else
                    //{
                    //    this.Invoke((MethodInvoker)delegate
                    //     {
                    //         btnDebugRun.Enabled = false;
                    //     });
                    //}
                });
                sub.IsBackground = true;
                sub.Start();
                mf.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("开始调试操作异常错误！", ex);
            }
        }

        /// <summary>
        /// 退出调试
        /// </summary>
        private void btnDebugExit_Click(object sender, EventArgs e)
        {
            try
            {
                mf.timer2.Enabled = false;
                mf.recBuffer.Clear();
                MainForm.isSuccess = false;
                mf.SendCmd(Protocol.GetDeviceDebugCmd(dType, dIndex, false));
                //Thread sub = new Thread(() =>
                //{
                //    if (!mf.CheckReturnCmd())
                //    {
                //        Messages.ShowAlertDialog("退出调试操作失败！", false);
                //    }
                //});
                //sub.IsBackground = true;
                //sub.Start();
                mf.timer2.Enabled = true;
                btnDebugRun.Enabled = true;
            }
            catch (Exception ex)
            {
                Messages.SaveLogDialog("退出调试操作异常错误！", ex);
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        private void btnDebugClear_Click(object sender, EventArgs e)
        {
            rtbDebugMessage.Clear();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void DebugInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            mf.timer2.Enabled = false;
            mf.recBuffer.Clear();
            mf.debugInfo.Dispose();
            mf.debugInfo = null;
            mf.SendCmd(Protocol.GetDeviceDebugCmd(dType, dIndex, false));
            Thread.Sleep(1000);
            mf.timer2.Enabled = true;
        }

        private void DebugInfo_Load(object sender, EventArgs e)
        {
            btnDebugRun_Click(null, null);
        }
    }
}
